package yannan.voiplock4ble.task;

import android.util.Log;

import com.amap.api.location.AMapLocation;
import com.amap.api.location.AMapLocationClient;
import com.amap.api.location.AMapLocationClientOption;
import com.amap.api.location.AMapLocationListener;

import yannan.voiplock4ble.BuildConfig;
import yannan.voiplock4ble.utils.AMapUtils;
import yannan.voiplock4ble.utils.OperLockUtils;

/**
 * 说明：上传门锁地址线程
 * 作者：mainTel
 * <p/>
 * 时间：2016/12/19 19:32
 * 备注：
 */
public class UPLocationTask extends Thread {

    String address;
    String LAL; //Latitude and longitude 经纬度 经度在前 纬度在后 逗号分隔

    public void setLockserialId(String lockserialId) {
        this.lockserialId = lockserialId;
    }

    String lockserialId;
    boolean b = true;
    //声明AMapLocationClient类对象
    public AMapLocationClient mLocationClient = null;
    int i = 0;

    @Override
    public void run() {
        initAMap();
        while (b && i < 60) {
            if (address == null || lockserialId == null) {
                if (BuildConfig.DEBUG) Log.e("UPLocationTask", "检查是否已经成功");
                try {
                    sleep(5000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            } else {
                OperLockUtils.upLoadLockAddress(lockserialId, address, LAL);
                mLocationClient.onDestroy();
                b = false;
            }
            i++;
        }
    }

    public void stopLocation() {
        b = false;
    }

    private void initAMap() {
        //初始化定位
        //设置定位回调监听
        mLocationClient.setLocationListener(mLocationListener);
        //初始化AMapLocationClientOption对象
        mLocationOption = new AMapLocationClientOption();
        //设置定位模式为AMapLocationMode.Hight_Accuracy，高精度模式。
        mLocationOption.setLocationMode(AMapLocationClientOption.AMapLocationMode.Hight_Accuracy);
        //true表示会主动刷新设备wifi模块，获取到最新鲜的wifi列表（wifi新鲜程度决定定位精度）；false表示不主动刷新。
        mLocationOption.setWifiScan(true);
        //获取一次定位结果：
        //该方法默认为false。
        mLocationOption.setOnceLocation(false);
        //设置定位间隔,单位毫秒,默认为2000ms，最低1000ms。
        mLocationOption.setInterval(5000);
        //获取最近3s内精度最高的一次定位结果：
        //设置setOnceLocationLatest(boolean b)接口为true，启动定位时SDK会返回最近3s内精度最高的一次定位结果。如果设置其为true，setOnceLocation(boolean b)接口也会被设置为true，反之不会，默认为false。
        mLocationOption.setOnceLocationLatest(true);
        //设置是否允许模拟位置,默认为false，不允许模拟位置
        mLocationOption.setMockEnable(true);
        //关闭缓存机制
        mLocationOption.setLocationCacheEnable(false);
        //给定位客户端对象设置定位参数
        mLocationClient.setLocationOption(mLocationOption);
        //启动定位
        mLocationClient.startLocation();
    }

    //声明AMapLocationClientOption对象
    public AMapLocationClientOption mLocationOption = null;

    public UPLocationTask(AMapLocationClient mLocationClient) {
        this.mLocationClient = mLocationClient;

    }


    //声明定位回调监听器
    public AMapLocationListener mLocationListener = new AMapLocationListener() {
        @Override
        public void onLocationChanged(AMapLocation aMapLocation) {
            if (null != aMapLocation) {
                //解析定位结果
                String result = AMapUtils.getLocationStr(aMapLocation);
                address = aMapLocation.getAddress();
                LAL = aMapLocation.getLongitude() + "," + aMapLocation.getLatitude();
                Log.e("UPLocationTask", LAL);
                Log.e("way", AMapUtils.getLocationStr(aMapLocation));
                //停止定位后，本地定位服务并不会被销毁
                mLocationClient.stopLocation();
            } else {
                mLocationClient.startLocation();
            }
        }
    };
}
